package com.tieunguyen.truyencuoi;

import java.util.ArrayList;

import com.tieunguyen.adapter.ListStoriesAdapter;
import com.tieunguyen.beans.Story;
import com.tieunguyen.util.DatabaseHelper;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class ListStoriesFragment extends Fragment {

	ViewGroup viewGroup;
	ListView listview;
	ListStoriesAdapter adapter;
	DatabaseHelper dbHelper;
	ArrayList<Story> stories = new ArrayList<Story>();
	int catId;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		catId = getArguments().getInt("catId");
	}

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		//return super.onCreateView(inflater, container, savedInstanceState);
		if (viewGroup == null){
			viewGroup = (ViewGroup)inflater.inflate(R.layout.list_stories, container, false);
			listview = (ListView)viewGroup.findViewById(R.id.listview);
			dbHelper = new DatabaseHelper(getActivity());
			stories = dbHelper.getStory(getActivity(), catId);
			adapter = new ListStoriesAdapter(getActivity(), stories);
			listview.setAdapter(adapter);
			listview.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> adapterView, View view,
						int position, long id) {
					// TODO Auto-generated method stub
					//dLayout.closeDrawers();
					Bundle bundle = new Bundle();
					bundle.putInt("storyId", stories.get(position).getId());
					bundle.putString("title", stories.get(position).getTitle());
					bundle.putString("content", stories.get(position).getDetail());
					Fragment detail = new DetailFragment();
					detail.setArguments(bundle);
					FragmentManager frm = getFragmentManager();
					FragmentTransaction transaction = frm.beginTransaction();
					transaction.replace(R.id.content_frame, detail);
					transaction.addToBackStack(null);
					transaction.commit();
					
					//Intent intent = new Intent(ListStoriesFragment.this.getActivity(), DetailActivity.class);
					//intent.putExtras(bundle);
					//startActivity(intent);
				}
			});
		}else{
			//((ViewGroup) viewGroup.getParent()).removeView(viewGroup);
		}
		return viewGroup;
	}

	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		super.onDestroyView();
		if (viewGroup != null) {
            ViewGroup parentViewGroup = (ViewGroup) viewGroup.getParent();
            if (parentViewGroup != null) {
                parentViewGroup.removeAllViews();
            }
        }
	}

	
}
